﻿using Microsoft.ManagementConsole.SnapIns.Serialization;
using Newtonsoft.Json;
using PlanckTime.Entity;
using System;
using Veripark.Data;
using Veripark.Mobile.ServiceProxy.Model;

namespace Microsoft.ManagementConsole.SnapIns.Integration
{
    public class PlanckTimeServices
    {
        #region Fields&Properties

        private static string SessionID;

        private ServiceIntegrationBase ServiceIntegration;

        private static PlanckTimeServices instance;

        public static PlanckTimeServices Instance
        {
            get
            {
                if (PlanckTimeServices.instance == null)
                {
                    PlanckTimeServices.instance = new PlanckTimeServices();
                }

                return PlanckTimeServices.instance;
            }
        }

        #endregion

        #region Methods

        private PlanckTimeServices()
        {
            ServiceIntegration = new ServiceIntegrationBase(PlanckTimeIntegrationConstants.PlanckTimeServicePath, new JsonSerialization());
        }

        public User Authenticate(User user)
        {
            var userParam = new { user = user };

            var serviceResponse = ServiceIntegration.CallMethod<ServiceRequest, ServiceResponse>(PlanckTimeIntegrationConstants.Authenticate, new ServiceRequest() { Header = new ServiceRequestHeader(), Parameters = userParam });

            SessionID = serviceResponse.Header.SessionId;

            return serviceResponse.Result as User;
        }

        public BasicEntity Save(BasicEntity entity)
        {
            var entityParam = new { entity = entity };

            var serviceResponse = ServiceIntegration.CallMethod<ServiceRequest, ServiceResponse>(PlanckTimeIntegrationConstants.Save, new ServiceRequest() { Header = new ServiceRequestHeader() { SessionId = SessionID }, Parameters = entityParam });

            return serviceResponse.Result as BasicEntity;
        }

        public BasicEntityCollection GetTasks(Task parent)
        {
            var parentParam = new { parent = parent };

            var serviceResponse = ServiceIntegration.CallMethod<ServiceRequest, ServiceResponse>("GetTasks", new ServiceRequest() { Header = new ServiceRequestHeader() { SessionId = SessionID }, Parameters = parentParam });

            return serviceResponse.Result as BasicEntityCollection;
        }

        public BasicEntityCollection GetTaskData(Guid taskId)
        {
            var task = new Task()
            {
                Id = taskId
            };
            
            var taskIdParam = new { task = task };

            var serviceResponse = ServiceIntegration.CallMethod<ServiceRequest, ServiceResponse>("GetTaskData", new ServiceRequest() { Header = new ServiceRequestHeader() { SessionId = SessionID }, Parameters = taskIdParam });

            return serviceResponse.Result as BasicEntityCollection;
        }

        public BasicEntityCollection GetEventLog(Guid taskId)
        {
            var task = new Task() 
            {
                Id=taskId
            };

            var taskIdParam = new { task = task };

            var serviceResponse = ServiceIntegration.CallMethod<ServiceRequest, ServiceResponse>("GetEventLog", new ServiceRequest() { Header = new ServiceRequestHeader() { SessionId = SessionID }, Parameters = taskIdParam });

            return serviceResponse.Result as BasicEntityCollection;
        }

        public string GetUserDisplayName(Guid userId)
        {
            var user = new User()
            {
                Id = userId
            };

            var userIdParam = new { user = user };

            var serviceResponse = ServiceIntegration.CallMethod<ServiceRequest, ServiceResponse>("GetUserDisplayName", new ServiceRequest() { Header = new ServiceRequestHeader() { SessionId = SessionID }, Parameters = userIdParam });

            return serviceResponse.Result as string;
        }
        #endregion
    }
}
